﻿using System.Reflection;
using System.Windows.Forms;
using EnvDTE;
using EnvDTE80;
using Microsoft.VisualStudio.CommandBars;

namespace VSTranslate.Commands
{
    internal sealed class CommandFactory
    {
        public static readonly CommandFactory Instance = new CommandFactory();
        object[] contextGuids = new object[] { };
        private Command enRuCommand;
        private Command ruEnCommand;
        private Command settingsCommand;
        private Command aboutCommand;
        private bool isFirstTime;

        private CommandFactory()
        {
        }

        public DTE2 DTE { get; private set; }
        public AddIn AddIn { get; private set; }

        public CommandBarPopup CreateTranslateMenuItem(CommandBarControls commandBarControls)
        {
            var translateMenuItem = (CommandBarPopup)
                                    commandBarControls.Add(MsoControlType.msoControlPopup,
                                                       Missing.Value,
                                                       Missing.Value, 1, true);
            translateMenuItem.Caption = "Google Translate";

            Instance.EnRuCommand.AddControl(translateMenuItem.CommandBar, 1);

            Instance.RuEnCommand.AddControl(translateMenuItem.CommandBar, 2);

            Instance.SettingsCommand.AddControl(translateMenuItem.CommandBar, 3);

            Instance.AboutCommand.AddControl(translateMenuItem.CommandBar, 4);

            return translateMenuItem;
        }

        public Command EnRuCommand
        {
            get
            {
                if (enRuCommand == null)
                {
                    var commands = (Commands2) DTE.Commands;
                    if (!isFirstTime)
                    {
                        var command = commands.Item("VSTranslate.Connect.VSTranslateEnRu", 0);
                        if (command != null)
                        {
                            return command;
                        }
                    }
                    else
                    {
                        //Add a command to the Commands collection:
                        enRuCommand = commands.AddNamedCommand2(AddIn, "VSTranslateEnRu", "Translate En-Ru",
                                                                "Executes the command for VSTranslate", true, 141,
                                                                ref contextGuids,
                                                                (int)vsCommandStatus.vsCommandStatusSupported +
                                                                (int)vsCommandStatus.vsCommandStatusEnabled,
                                                                (int)vsCommandStyle.vsCommandStylePictAndText,
                                                                vsCommandControlType.vsCommandControlTypeButton);
                    }
                }
                return enRuCommand;
            }
        }

        public Command SettingsCommand
        {
            get
            {
                if (settingsCommand == null)
                {
                    var commands = (Commands2) DTE.Commands;
                    if (!isFirstTime)
                    {
                        var command = commands.Item("VSTranslate.Connect.VSTranslateSettings", 0);
                        if (command != null)
                        {
                            return command;
                        }
                    }
                    else
                    {
                        //Add a command to the Commands collection:
                        settingsCommand = commands.AddNamedCommand2(AddIn, "VSTranslateSettings", "Settings...",
                                                                "Executes the command for VSTranslate", true, 59,
                                                                ref contextGuids,
                                                                (int)vsCommandStatus.vsCommandStatusSupported +
                                                                (int)vsCommandStatus.vsCommandStatusEnabled,
                                                                (int)vsCommandStyle.vsCommandStylePictAndText,
                                                                vsCommandControlType.vsCommandControlTypeButton);
                    }
                }
                return settingsCommand;
            }
        }

        public Command AboutCommand
        {
            get
            {
                if (aboutCommand == null)
                {
                    var commands = (Commands2) DTE.Commands;
                    if (!isFirstTime)
                    {
                        var command = commands.Item("VSTranslate.Connect.VSTranslateAbout", 0);
                        if (command != null)
                        {
                            return command;
                        }
                    }
                    else
                    {
                        //Add a command to the Commands collection:
                        aboutCommand = commands.AddNamedCommand2(AddIn, "VSTranslateAbout", "About...",
                                                                "Executes the command for VSTranslate", true, 59,
                                                                ref contextGuids,
                                                                (int)vsCommandStatus.vsCommandStatusSupported +
                                                                (int)vsCommandStatus.vsCommandStatusEnabled,
                                                                (int)vsCommandStyle.vsCommandStylePictAndText,
                                                                vsCommandControlType.vsCommandControlTypeButton);
                    }
                }
                return aboutCommand;
            }
        }

        public Command RuEnCommand
        {
            get
            {
                if (ruEnCommand == null)
                {
                    var commands = (Commands2) DTE.Commands;
                    if (!isFirstTime)
                    {
                        Command command = commands.Item("VSTranslate.Connect.VSTranslateRuEn", 0);
                        if (command != null)
                        {
                            return command;
                        }
                    }
                    else
                    {
                        //Add a command to the Commands collection:
                        ruEnCommand = commands.AddNamedCommand2(AddIn, "VSTranslateRuEn", "Translate Ru-En",
                                                                "Executes the command for VSTranslate", true, 141,
                                                                ref contextGuids,
                                                                (int) vsCommandStatus.vsCommandStatusSupported +
                                                                (int) vsCommandStatus.vsCommandStatusEnabled,
                                                                (int) vsCommandStyle.vsCommandStylePictAndText,
                                                                vsCommandControlType.vsCommandControlTypeButton);
                    }
                }
                return ruEnCommand;
            }
        }

        public void Init(DTE2 dte, AddIn addIn, bool isFirstTime)
        {
            DTE = dte;
            AddIn = addIn;
            this.isFirstTime = isFirstTime;
            if (isFirstTime)
            {
                CreateCommands();
            }
        }

        private void CreateCommands()
        {
            EnRuCommand.GetType();
            RuEnCommand.GetType();
            SettingsCommand.GetType();
            AboutCommand.GetType();
        }
    }
}